﻿using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    [DataContract]
    public class WDominoType : NotificationObject
    {
        private int    dominoTypeUID;
        private String dominoTypeName;
        private String dominoImage;
        private String dominoVolume;
        private String company;
        private String hasee;

        [DataMember(Order = 0)]
        public int DominoTypeUID
        {
            get
            {
                return dominoTypeUID;
            }
            set
            {
                dominoTypeUID = value;
                RaisePropertyChanged("DominoTypeUID");
            }
        }

        [DataMember(Order = 1)]
        public String DominoTypeName
        {
            get
            {
                return dominoTypeName;
            }
            set
            {
                dominoTypeName = value;
                RaisePropertyChanged("DominoTypeName");
            }
        }

        [DataMember(Order = 2)]
        public String DominoImage
        {
            get
            {
                return dominoImage;
            }
            set
            {
                dominoImage = value;
                RaisePropertyChanged("DominoImage");
            }
        }

        [DataMember(Order = 3)]
        public String DominoVolume
        {
            get
            {
                return dominoVolume;
            }
            set
            {
                dominoVolume = value;
                RaisePropertyChanged("DominoVolume");
            }
        }

        [DataMember(Order = 4)]
        public String Company
        {
            get
            {
                return company;
            }
            set
            {
                company = value;
                RaisePropertyChanged("Company");
            }
        }

        [DataMember(Order = 5)]
        public String Hasee
        {
            get
            {
                return hasee;
            }
            set
            {
                hasee = value;
                RaisePropertyChanged("Hasee");
            }
        }

        public WDominoType()
        {
            DominoTypeUID = -1;
            DominoTypeName = "";
            DominoImage = "";
            DominoVolume = "";
            Company = "";
            Hasee = "";
        }

        public WDominoType(LDominoType p)
        {
            DominoTypeUID = p.DominoTypeUID;
            DominoTypeName = p.DominoTypeName;
            DominoImage = p.DominoImage;
            DominoVolume = p.DominoVolume;
            Company = p.Company;
            Hasee = p.Hasee;
        }

        public static int GetDominoTypeUID(String pDominoTypeName)
        {
            HttpResponse res = FridgeConnector.PostMessage(EStudy.DominoType, EMethod.SelectAll, "");

            if (res.Status != "Success")
            {
                return -1;
            }

            List<WDominoType> list = JSON.parse<List<WDominoType>>(res.Data1);
            foreach (var item in list)
            {
                if (item.DominoTypeName.Trim() == pDominoTypeName.Trim())
                {
                    return item.DominoTypeUID;
                }
            }

            return -1;
        }

        public static List<WDominoType> GetAll()
        {
            HttpResponse res = FridgeConnector.PostMessage(EStudy.DominoType, EMethod.SelectAll, "");
            return JSON.parse<List<WDominoType>>(res.Data1);
        }

        public static HttpResponse Insert(List<WDominoType> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.DominoType, EMethod.Insert, postData);
        }

        public static HttpResponse Update(List<WDominoType> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.DominoType, EMethod.Update, postData);
        }

        public static HttpResponse Delete(List<WDominoType> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.DominoType, EMethod.Delete, postData);
        }
    }
}
